<?php
// вспомогательные функции для отладки
function ppr($value)
{
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}

function ppre($v)
{
    ppr($v);
    exit;
}

//////////////////////////////////////


// Обработчик создания формы
function formulaires_main_charger_dist()
{
    // Получение информации о таблице , что бы отобразить её поля
    $table_description = description_table('spip_articles');
    // Получаем поля таблицы
    $fields = '';


    if (isset($table_description['field']))
    foreach ($table_description['field'] as $key=>$values)
    {
        $fields[$key]= $key;
    }



    $values = array('table_fields'=>$fields);

    // Возвращаем массив данных для формы
    return $values;

}

// Проверка входящих данных
function formulaires_main_verifier_dist()
{

    // Проверка существования критериев по удалению.
    $table_field = _request('table_field');
    $u_table_field = _request('u_table_field');
    $keywords    = _request('keywords');
    $type_search = _request('type_search');
    $action = _request('z_action');

    $error_message = array();

    if ((!$table_field)&&$type_search == 'table_key')
    {
        $error_message['table_field'] = _T('massupdater:e_table_fields');
    }
    
    if (($action == 'change_by_fields')&&(!$u_table_field))
    {
        $error_message['error_all'] = _T('massupdater:e_u_table_fields');
    }
    
    

    if (!$keywords)
    {
        $error_message['keywords'] = _T('massupdater:e_keywords');
    }

    if (!$type_search)
    {
        $error_message['error_all'] = _T('massupdater:e_type_search');
    }

    if (!$action)
    {
        $error_message['error_all'] = _T('massupdater:e_action');
    }



    return $error_message;

}

// Выполнениу назначенного процесса
function formulaires_main_traiter_dist()
{


    $table_field = _request('table_field');
    $keywords    = _request('keywords');
    $type_search = _request('type_search');
    $action =      _request('z_action');
    $change_value = trim(_request('change_value'));
    $u_table_field = _request('u_table_field');

    $mas = explode("\n",$keywords);
    $i = 0;
    $where = '';

   
    
    switch ($type_search)
    {
        case  'table_key':
            
            
             // Составляем условие запроса
        foreach ($mas as $value)
        {
            if ($i==0)
            {
                $where.=' `a`.`'.$table_field.'` like "%'.trim($value).'%"';
            }
            else
            {
                $where.=' OR  `a`.`'.$table_field.'` like "%'.trim($value).'%" ';
            }
            $i++;
        }
        
        switch ($action)
        {
            case 'delete_by_status':
                    $sql = 'UPDATE spip_articles,(select a.id_article from spip_articles a where '.$where.') as T set spip_articles.statut="poubelle" where spip_articles.id_article=T.id_article';
                break;
            case 'change_by_fields':
                    $sql = 'UPDATE spip_articles,(select a.id_article from spip_articles a where '.$where.') as T set spip_articles.'.$u_table_field.'="'.$change_value.'" where spip_articles.id_article=T.id_article';
                break;
            case 'delete_by_criteria':
                    $sql = 'DELETE spip_articles where spip_articles.id_article in (select a.id_article from spip_articles a where '.$where.')';
                break;
        }
        
        
 
            
            
            break;
        case 'id_mot':
             // Составляем условие запроса
            foreach ($mas as $value)
            {
                if ($i==0)
                    {
                        $where .=' `L1.id_mot` = '.trim($value);
                    }
                else
                    {
                        $where.=' OR  `L1.id_mot` = '.trim($value).' ';
                    }
                $i++;
            }
            
             switch ($action)
            {
            case 'delete_by_status':
                    $sql = 'UPDATE spip_articles,(select a.id_article from spip_articles a inner join spip_mots_liens AS L1 on  L1.id_objet = articles.id_article AND L1.objet="article" where '.$where.') as T set spip_articles.statut="poubelle" where spip_articles.id_article=T.id_article';
                break;
            case 'change_by_fields':
                    $sql = 'UPDATE spip_articles,(select a.id_article from spip_articles a inner join spip_mots_liens AS L1 on  L1.id_objet = articles.id_article AND L1.objet="article" where '.$where.') as T set '.$u_table_field.'="'.$change_value.'" where spip_articles.id_article=T.id_article';
                break;
            case 'delete_by_criteria':
                    $sql = 'DELETE spip_articles where spip_articles.id_article in (select a.id_article from spip_articles a inner join spip_mots_liens AS L1 on  L1.id_objet = articles.id_article AND L1.objet="article" where '.$where.')';
                break;
            }
            
            
            
            break;
    }
    
    
    
    
    
  // Составляем SQL
    

   // echo $sql.'<br>';
    
    // Выполняем sql
        $count = 0;
        $res = sql_query($sql) ;
        $count = mysql_affected_rows();



        // Выводим результат
        if ($res)
        {
            $txt =  _T('massupdater:res_ok1').$count._T('massupdater:res_ok2').'<br><br>';
        }
        else
        {
            $txt = _T('massupdater:result_empty');
        }



    return array('message_ok'=>$txt);


}



?>